var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/businessComponent/work-flow/WorkFlowStartWin.tsx
var WorkFlowStartWin_exports = {};
__export(WorkFlowStartWin_exports, {
  openWorkFlowStartWin: () => openWorkFlowStartWin
});
module.exports = __toCommonJS(WorkFlowStartWin_exports);
var import_react = require("react");
var import_baseComponent = require("../../baseComponent");
var import_functionalComponent = require("../../functionalComponent");
var import_util = require("../../util");
var import_service = require("./service");
var import_util2 = require("./util");
var import_WFDynamicBranchWin = require("./WFDynamicBranchWin");
var import_WFDynamicNodeUserWin = require("./WFDynamicNodeUserWin");
var import_WFNodeUserSettingWin = require("./WFNodeUserSettingWin");
var global = {
  workInFlow: null,
  close: import_util.emptyFn,
  modalIns: null
};
function fuzzyMatchObject(filterDataObj, searchStr, keysToMatch) {
  const pattern = new RegExp(searchStr, "i");
  for (const key of keysToMatch) {
    const value = filterDataObj[key];
    if (value && pattern.test(value)) {
      return true;
    }
  }
  return false;
}
async function startFlow(step) {
  var _a, _b, _c, _d, _e, _f;
  const { workInFlow } = global;
  console.log(global, "global");
  if (step === 1) {
    const resp = await (0, import_functionalComponent.usingProgress)(
      () => (0, import_service.checkPreStartProcessInstance)({
        processDefinitionId: workInFlow.startFlowParam.processDefinitionId,
        formData: {
          appCode: workInFlow.appCode,
          bizCode: workInFlow.bizCode,
          dataId: workInFlow.dataId,
          cuNo: workInFlow.cuNo,
          orgNo: workInFlow.orgNo,
          processType: workInFlow.processType,
          formApprovalUserNo: workInFlow.formApprovalUserNo
        }
      })
    );
    if (resp.code === 0) {
      workInFlow.dynamicInfo = resp.data;
      workInFlow.startFlowParam.nodeName = resp.data.nodeName;
      const { state, message } = (_a = resp.data) == null ? void 0 : _a.bizOperationCheckResult;
      if (state === 2) {
        await (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, message);
        (0, import_util2.reloadPage)();
      } else if (state === 1) {
        if (await (0, import_util2.wfConfirm)(import_util2.NGLang.alertTitle, message)) {
          startFlow(2);
        } else {
          (0, import_util2.reloadPage)();
        }
      } else {
        if (!workInFlow.dynamicInfo.dynamicFlowNodes && !workInFlow.dynamicInfo.dynamicUserNodes) {
          startFlow(4);
        } else {
          startFlow(2);
        }
      }
    } else {
      await (0, import_util2.wfAlert)(import_util2.NGLang.getTaskDynamicError, resp.message);
    }
  } else if (step === 2) {
    if (workInFlow.dynamicInfo.dynamicFlowNodes && Object.keys(workInFlow.dynamicInfo.dynamicFlowNodes).length > 0) {
      const callBack = (data) => {
        var _a2;
        workInFlow.startFlowParam.dynamicBranches = data;
        workInFlow.startFlowParam.nodeName = (_a2 = data[0]) == null ? void 0 : _a2.nodeName;
        startFlow(3);
      };
      const branchData = [];
      branchData.push(workInFlow.dynamicInfo.dynamicFlowNodes);
      await (0, import_util2.wfModal)({
        title: import_util2.NGLang.dynamicBranchWinTitle,
        width: 400,
        content: /* @__PURE__ */ React.createElement(import_WFDynamicBranchWin.WFDynamicBranchWin, { branchData, callBack })
      });
    } else {
      await startFlow(3);
    }
  } else if (step === 3) {
    let nodeData = [];
    if (workInFlow.dynamicInfo.dynamicUserNodes && workInFlow.dynamicInfo.dynamicUserNodes.length > 0) {
      const targetIdList = ((_b = workInFlow.startFlowParam) == null ? void 0 : _b.dynamicBranches) ? (_c = workInFlow.startFlowParam) == null ? void 0 : _c.dynamicBranches.map((i) => i.targetNodeId) : [];
      const userNodes = workInFlow.dynamicInfo.dynamicUserNodes;
      const result = (0, import_util2.filterUserNodes)(targetIdList, userNodes, "dependAssignedNodes");
      nodeData = result;
      console.log(nodeData, "nodeData");
    }
    if (nodeData.length === 1) {
      const _nodeData = nodeData[0];
      const radioItems = (0, import_util2.getUseDynamicType)(_nodeData.users.length > 0, _nodeData.assignAnyUsers);
      const callBack = (data) => {
        var _a2;
        const minUserCount = workInFlow.dynamicInfo.dynamicUserNodes.filter((i) => i.id === _nodeData.id)[0].minUserCount;
        workInFlow.startFlowParam.dynamicNodeUsers = [
          {
            nodeId: _nodeData.id,
            userNos: data.map((_d2) => _d2.userNo),
            whetherCrossNode: _nodeData.whetherCrossNode
            //是否是跨节点这个节点
          }
        ];
        if (data.length < minUserCount) {
          (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, (0, import_util2.stringFormat)(import_util2.NGLang.createFlowMinUserCount, minUserCount));
          (_a2 = global.close) == null ? void 0 : _a2.call(global);
        }
        startFlow(4);
      };
      await (0, import_util2.wfModal)({
        title: `${_nodeData.name}${import_util2.NGLang.taskHisNode}--${import_util2.NGLang.dynamicUsers}`,
        width: 900,
        footerLeft: /* @__PURE__ */ React.createElement(import_WFDynamicNodeUserWin.WFDynamicNodeUserWinFooter, { radioItems }),
        content: /* @__PURE__ */ React.createElement(
          import_WFDynamicNodeUserWin.WFDynamicNodeUserWin,
          {
            radioItems,
            loginInfo: workInFlow.data.loginOrgInfo,
            todoData: _nodeData.users,
            callBack,
            cancelBack: import_util2.reloadPage
          }
        )
      });
    } else if (nodeData.length > 1) {
      const callBack = (data) => {
        workInFlow.startFlowParam.dynamicNodeUsers = workInFlow.startFlowParam.dynamicNodeUsers.concat(data).map((i, index) => {
          return {
            ...i,
            whetherCrossNode: workInFlow.dynamicInfo.dynamicUserNodes[index].whetherCrossNode,
            name: workInFlow.dynamicInfo.dynamicUserNodes[index].name
          };
        });
        startFlow(4);
      };
      await (0, import_util2.wfModal)({
        title: import_util2.NGLang.nodeDynamicUsers,
        width: 650,
        content: /* @__PURE__ */ React.createElement(import_WFNodeUserSettingWin.WFNodeUserSettingWin, { nodeData, loginOrgInfo: workInFlow.data.loginOrgInfo, callBack })
      });
    } else {
      await startFlow(4);
    }
  } else if (step === 4) {
    const resp = await (0, import_functionalComponent.usingProgress)(
      () => import_util.iup.request.body({
        url: "workflow/process/startProcessInstance",
        data: workInFlow.startFlowParam
      }),
      { title: `${import_util2.NGLang.createFlow}...` }
    );
    (_d = global.close) == null ? void 0 : _d.call(global);
    if (resp.code === 0) {
      await (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, import_util2.NGLang.createFlowSuccess);
      (_e = workInFlow.callback) == null ? void 0 : _e.call(workInFlow, resp.data);
    } else {
      await (0, import_util2.wfAlert)(import_util2.NGLang.createFlowError, resp.message);
      (_f = workInFlow.cancelback) == null ? void 0 : _f.call(workInFlow);
    }
  }
}
function WorkFlowStartWin() {
  const { workInFlow } = global;
  const tableRef = (0, import_util.useApi)();
  const mCtx = (0, import_react.useContext)(import_functionalComponent.ModalContext);
  const [form, setForm] = (0, import_react.useState)({ txtRemark: "", signature: { code: "", url: "" } });
  const onSearchHandler = (0, import_util.useRefCallback)((value) => {
    tableRef.current.getApi().filter((item) => fuzzyMatchObject(item, value, ["id", "name"]));
  });
  const validData = (0, import_util.useRefCallback)(() => {
    const txtRemark = form.txtRemark.trim();
    const minCommentLen = workInFlow.minCommentLen;
    if (minCommentLen > 0 && !txtRemark) {
      (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, import_util2.NGLang.hasNoremark).then();
      return false;
    }
    if (minCommentLen > txtRemark.length) {
      (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, (0, import_util2.stringFormat)(import_util2.NGLang.reamrkHasMore, minCommentLen)).then();
      return false;
    }
    if (workInFlow.needsignature && !form.signature.code) {
      (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, import_util2.NGLang.mustSignature).then();
      return false;
    }
    return true;
  });
  const validDataAndStartFlow = (0, import_util.useRefCallback)(async () => {
    const selectData = tableRef.current.getApi().getSelectedData()[0];
    if (!selectData) {
      await (0, import_util2.wfAlert)(import_util2.NGLang.alertTitle, import_util2.NGLang.selectOnePd);
      return;
    }
    const processActivityData = await (0, import_service.getProcessInitialActivity)(selectData.id);
    selectData.mincommentlen = processActivityData.minCommentLength;
    selectData.mustsignature = processActivityData.requiredSignature;
    workInFlow.minCommentLen = Number(selectData.mincommentlen) || 0;
    workInFlow.needsignature = selectData.mustsignature;
    if (!validData()) {
      return;
    }
    workInFlow.startFlowParam.processDefinitionId = selectData.id;
    workInFlow.startFlowParam.formData = {};
    workInFlow.startFlowParam.formData.bizCode = workInFlow.bizCode;
    workInFlow.startFlowParam.formData.dataId = workInFlow.dataId;
    workInFlow.startFlowParam.formData.cuNo = workInFlow.cuNo;
    workInFlow.startFlowParam.formData.orgNo = workInFlow.orgNo;
    workInFlow.startFlowParam.formData.processType = workInFlow.processType;
    workInFlow.startFlowParam.formData.formApprovalUserNo = workInFlow.formApprovalUserNo;
    workInFlow.startFlowParam.comment = {};
    workInFlow.startFlowParam.comment.content = form.txtRemark;
    workInFlow.startFlowParam.comment.signatureId = form.signature.code;
    workInFlow.startFlowParam.dynamicNodeUsers = [];
    workInFlow.startFlowParam.dynamicBranches = [];
    await startFlow(1);
  });
  (0, import_react.useEffect)(() => {
    global.modalIns = mCtx.ins;
    mCtx.ins.setApi({
      invokeOkHandler: validDataAndStartFlow,
      invokeCancelHandler: async () => {
        var _a, _b, _c;
        await ((_b = (_a = global.workInFlow).cancelback) == null ? void 0 : _b.call(_a));
        (_c = global.close) == null ? void 0 : _c.call(global);
      }
    });
    tableRef.current.getApi().setSelected(0);
  }, []);
  const compProps = (0, import_react.useMemo)(() => {
    const iconStyle = { fontSize: 14, cursor: "pointer" };
    const columnProps = {
      sortable: false,
      resizable: false,
      columnSort: false
    };
    return {
      labelStyle: {
        width: 65,
        marginLeft: "6px",
        display: "flex",
        flexDirection: "column",
        justifyContent: "center"
      },
      tableProps: {
        headerHeight: 32,
        rowHeight: 30,
        style: { border: "1px solid var(--border-color-split, #f0f0f0)" },
        columns: [
          {
            ...columnProps,
            header: import_util2.NGLang.pdId,
            dataIndex: "id"
          },
          {
            ...columnProps,
            header: import_util2.NGLang.pdName,
            tooltip: true,
            dataIndex: "name"
          },
          {
            ...columnProps,
            header: import_util2.NGLang.description,
            tooltip: true,
            dataIndex: "description"
          },
          // {
          //   ...columnProps,
          //   header: NGLang.btnView,
          //   width: 50,
          //   align: 'center',
          //   dataIndex: 'view',
          //   render({ row }) {
          //     return <FileSearchOutlined style={iconStyle} onClick={() => showPdDiagram(row.id)} />;
          //   }
          // },
          {
            ...columnProps,
            header: import_util2.NGLang.operation,
            width: 200,
            align: "center",
            dataIndex: "test",
            render({ row }) {
              return /* @__PURE__ */ React.createElement("a", { style: iconStyle, onClick: () => (0, import_util2.showPdDiagram)(row, workInFlow) }, import_util2.NGLang.btnTest);
            }
          }
        ],
        dataSource: workInFlow.data.processDefinitions || []
      }
    };
  }, []);
  const onRemarkHandler = (txtRemark) => {
    setForm((p) => ({ ...p, txtRemark }));
  };
  const onSignatureChange = (signature) => {
    setForm((p) => ({ ...p, signature }));
  };
  return /* @__PURE__ */ React.createElement(import_util.Layout, { style: { height: 400, padding: "5px 12px" } }, /* @__PURE__ */ React.createElement(import_util.Layout, { direction: "row" }, /* @__PURE__ */ React.createElement("label", { style: compProps.labelStyle }, import_util2.NGLang.taskRemark, ":"), /* @__PURE__ */ React.createElement(
    import_baseComponent.TextArea,
    {
      value: form.txtRemark,
      onChange: onRemarkHandler,
      rows: 3,
      style: { flex: 1, resize: "none", marginTop: "12px" }
    }
  )), /* @__PURE__ */ React.createElement(import_util.Layout, { direction: "row", style: { width: "90%", margin: "12px 0" } }, /* @__PURE__ */ React.createElement("label", { style: compProps.labelStyle }, import_util2.NGLang.procDefin, ":"), /* @__PURE__ */ React.createElement(import_baseComponent.Search, { placeholder: import_util2.NGLang.seachEmptyText, allowClear: true, onSearch: onSearchHandler })), /* @__PURE__ */ React.createElement(import_util.Layout.Flex, null, /* @__PURE__ */ React.createElement(import_functionalComponent.Table, { ref: tableRef, ...compProps.tableProps })));
}
async function openWorkFlowStartWin({ autoStart, ...props }) {
  global.workInFlow = props;
  global.workInFlow.startFlowParam = {};
  global.close = () => {
    var _a;
    global.close = import_util.emptyFn;
    (_a = global.modalIns) == null ? void 0 : _a.destroy();
    global.modalIns = null;
  };
  await (0, import_util2.wfModal)({
    title: import_util2.NGLang.createFlowWinTitile,
    content: /* @__PURE__ */ React.createElement(WorkFlowStartWin, null),
    width: 750
  });
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  openWorkFlowStartWin
});
